<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery1.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="moveOut">流出</el-menu-item>
      <el-menu-item index="moveIn">加入</el-menu-item>
    </el-menu>

    <div style="margin-top:40px">
      <el-form :inline="true" :model="listQuery1" size="mini" label-width="100px">
        <span v-if="listQuery1.tab == 'moveIn'">
          <el-form-item label="加入时间：">
            <el-date-picker
              v-model="listQuery1.moveDate"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="加入后负责人：">
            <el-cascader
              size="mini"
              collapse-tags
              :show-all-levels="false"
              v-model="listQuery1.userIds"
              placeholder="请选择部门/事业部/人员"
              :options="userList"
              :props="props"></el-cascader>
          </el-form-item>
          <el-form-item label="加入原因：">
            <el-input  v-model="listQuery1.moveRemark"></el-input>
          </el-form-item>
        </span>
        <span v-else>
          <el-form-item label="流出时间：">
            <el-date-picker
              v-model="listQuery1.moveDate"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="流出前负责人：">
            <el-cascader
              size="mini"
              collapse-tags
              :show-all-levels="false"
              v-model="listQuery1.userIds"
              placeholder="请选择部门/事业部/人员"
              :options="userList"
              :props="props"></el-cascader>
          </el-form-item>
          <el-form-item label="流出原因：">
            <el-select v-model="listQuery1.moveRemark" clearable filterable>
               <el-option
                 v-for="(item,index) in moveOutList"
                 :key="index"
                 :label="item.cddValue"
                 :value="item.cddValue">
            </el-option>
            </el-select>
          </el-form-item>
        </span>
        <el-form-item>
          <el-button
            @click="handleSearchList1()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="listLoading1"
        :data="tableData1"
        max-height="460"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="客户名称" min-width="200" align="center">
          <template slot-scope="scope">
            <div>{{scope.row.customerNumber}}</div>
            <div v-if="scope.row.isShowCustomerName" style="white-space: normal;">{{scope.row.customerName}}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="最近跟进时间"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.lastTrackingTime}}
          </template>
        </el-table-column>
        <el-table-column
          label="最近成交时间"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            {{scope.row.lastOrderTime}}
          </template>
        </el-table-column>
        <el-table-column
          :label="listQuery1.tab == 'moveIn' ? '加入时间':'流出时间'"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.moveTime}}
          </template>
        </el-table-column>
        <el-table-column
          :label="listQuery1.tab == 'moveIn' ? '加入原因':'流出原因'"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.moveRemark}}
          </template>
        </el-table-column>
        <el-table-column
          :label="listQuery1.tab == 'moveIn' ? '加入后负责人':'流出前负责人'"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.chargeName}}
          </template>
        </el-table-column>
        <el-table-column
          label="现负责人"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.nowChargeName}}
          </template>
        </el-table-column>

      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange1"
        @current-change="handleCurrentChange1"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery1.pageNum"
        :page-size="listQuery1.pageSize"
        :page-sizes="[10,15,20]"
        :total="count1">
      </el-pagination>
    </div>

  </div>
</template>
<script>
import {saleUserList,customerMoveList} from '@/api/data.js'
import {getConfigList} from '@/api/partner.js'

import {tongXingLogForms} from '@/api/fms.js'
import store from '@/store'
import { mapGetters } from 'vuex'
import {getDeptList} from '@/api/management'
import { formatDate } from '@/utils/date.js';

const defaultListQuery={
  "time" : [],//时间
  "userIds" : [],//业务员
  tab:'moveOut',//流转类型(流入:moveIn   流出：moveOut)
  moveDate:[],
  moveRemark:''
};



export default {
  name: "index",
  data(){
    return {
      roles:store.getters.roles ? store.getters.roles : [],
      props:{multiple: true,label:'value',value:'label',emitPath:false},
      listQuery1: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData1: [],
      count1: 0,
      listLoading1: false,
      data:[],
      userList:[],
      moveOutList:[]

    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  created() {
    this.getUserList()
    this.listQuery1.moveDate = this.getMonth()
    if(this.$route.query.dataMonth){
      this.listQuery1.moveDate = this.getMonth(this.$route.query.dataMonth)
    }
    if(this.$route.query.dataTab){
      this.listQuery1.tab = this.$route.query.dataTab
    }
    if(this.$route.query.dataUserIds){
      this.listQuery1.userIds = this.$route.query.dataUserIds.split(",")
    }
    if(this.$route.query.moveDate){
      this.listQuery1.moveDate = [this.$route.query.moveDate,this.$route.query.moveDate]
    }
    this.getList1();
    this.getMoveOutList();
  },
  filters: {


  },
  methods:{

    getMoveOutList(){
      getConfigList({status:1}).then((res)=>{
        let moveOutList = res.find((el)=>{
          return  el.cdtCode == 'customer_move_out_reason'
        })
        this.moveOutList = moveOutList ? moveOutList.data : []
      })
    },
    //获取本月时间
    getMonth(cutmonth){
      let now = new Date();
      if(cutmonth){
        now = new Date(cutmonth+'-01');
      }

      // 获取当前月份和年份
      const month = now.getMonth();
      const year = now.getFullYear();
      // 计算本月第一天的日期
      const startOfMonth = new Date(year, month, 1);
      // 计算下个月第一天的日期，然后减去一天得到本月的最后一天
      const startOfNextMonth = new Date(year, month + 1, 1);
      const endOfMonth = new Date(startOfNextMonth);
      endOfMonth.setDate(startOfNextMonth.getDate() - 1);
      return [formatDate(new Date(startOfMonth), 'yyyy-MM-dd'),formatDate(new Date(endOfMonth), 'yyyy-MM-dd')]
    },
    getUserList(){
      saleUserList().then((res)=>{
        this.userList = res
      })
    },
    handleSelect(key){
      this.listQuery1.tab = key
      this.handleSearchList1()
    },


    //获取列表
    getList1(){
      this.listLoading1 = true;
      let listQuery = JSON.parse(JSON.stringify(this.listQuery1));
      listQuery.userIds=listQuery.userIds.join(",")
      customerMoveList(listQuery).then(response => {
        this.tableData1 = response.list;
        this.count1 = response.count;
        this.listLoading1 = false;
      }).catch(()=>{
        this.listLoading1 = false;
      });
    },

    handleSizeChange1(val){
      this.listQuery1.pageNum = 1;
      this.listQuery1.pageSize = val;
      this.getList1();
    },
    handleCurrentChange1(val){
      this.listQuery1.pageNum = val;
      this.getList1();
    },

    handleSearchList1(){
      this.listQuery1.pageNum = 1;
      this.getList1();
    },


  },
}
</script>


